<%-- 
    Document   : lab
    Created on : 2019-2-17, 16:53:57
    Author     : dusin
data-options="events:{blur:calMinusImpurity},min:0,precision:4"   blur事件不好用
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%
    session.setAttribute("url", "../main/datamanager.jsp");
%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>        
        <link rel="stylesheet" href="../../js/jquery/themes/default/easyui.css" type="text/css" />
        <link rel="stylesheet" href="../../js/jquery/themes/icon.css" type="text/css" />
        <link rel="stylesheet" href="../../js/jquery/themes/color.css" type="text/css" />
        <link rel="stylesheet" href="../../css/demo.css" type="text/css" />
        <script type="text/javascript" src="../../js/jquery/jquery.min.js"></script>
        <script type="text/javascript" src="../../js/jquery/jquery.easyui.min.js"></script>
        <script type="text/javascript" src="../../js/jquery/locale/easyui-lang-zh_CN.js"></script>
        <script type="text/javascript">
            //检查输入项是否存在于下拉框选项中
            function checkComboBoxValue(id) {
                var value = $(id).combobox('getValue');
                var valueField = $(id).combobox("options").valueField;
                var allData = $(id).combobox("getData");
                var result = false;
                for (var i = 0; i < allData.length; i++) {
                    if (value == allData[i][valueField]) {
                        result = true;
                        break;
                    }
                }
                return result;
            }

            $.extend($.fn.validatebox.defaults.rules, {
                selectValueRequired: {
                    validator: function(value, param) {
                        //console.info($(param[0]).find("option:contains('" + value + "')").val());
                        return checkComboBoxValue(param[0]);
                    },
                    message: '该输入项为必选项'
                },
                /*10:05:02*/
                time: {
                    validator: function(value) {
                        var a = value.match(/^(\d{1,2})(:)?(\d{1,2})\2(\d{1,2})$/);
                        if (a == null) {
                            return false;
                        } else if (a[1] > 24 || a[3] > 60 || a[4] > 60) {
                            return false;
                        }
                    },
                    message: '时间格式不正确，请重新输入。'
                },
                /*2017-02-01*/
                date: {
                    validator: function(value) {
                        var r = value.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/);
                        if (r == null) {
                            return false;
                        }
                        var d = new Date(r[1], r[3] - 1, r[4]);
                        return (d.getFullYear() == r[1] && (d.getMonth() + 1) == r[3] && d.getDate() == r[4]);
                    },
                    message: '时间格式不正确，请重新输入。'
                },
                /*2014-01-01 13:04:06*/
                datetime: {
                    validator: function(value) {
                        var r = value.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/);
                        if (r == null)
                            return false;
                        var d = new Date(r[1], r[3] - 1, r[4], r[5], r[6], r[7]);
                        return (d.getFullYear() == r[1] && (d.getMonth() + 1) == r[3] && d.getDate() == r[4] && d.getHours() == r[5] && d.getMinutes() == r[6] && d.getSeconds() == r[7]);
                    },
                    message: '时间格式不正确，请重新输入(2020-01-01 13:04:06)。'
                }

            });
            $(document).ready(function() {
                $('#dgstudent').datagrid({
                    pagination: true,
                    pageList: [20, 50, 100],
                    pageSize: 20,
                    height: ($(window).height() - 3) + 'px'
                });
                $("#upload-photo").click(function() {
                    $('#dlg-upload').dialog('close');                    
                    $('#dgstudent').datagrid('loading');                    

                    $("#fm-upload").form('submit', {
                        type: "post", //提交方式    
                        dataType: 'json',
                        url: "../../servlet/web/cargo/Upload4Photo", //请求url
                        onSubmit: function(param) {
                           
                            var v1 = $('#file1').textbox('getValue');
                            var v2 = $('#file2').textbox('getValue');
                            var v3 = $('#file3').textbox('getValue');
                            var v4 = $('#file4').textbox('getValue');
                            var v = v1||v2||v3||v4;
                            if (v === null || v === "") {
                                $.messager.alert({title: '提示',
                                    msg: '请选择一个文件后再上传',
                                    icon: 'info',
                                });
                                return false;
                            }
                        },
                        success: function(data) { //提交成功的回调函数   
                            var data = eval('(' + data + ')');
                            $.messager.alert({title: '提示',
                                msg: data.msg,
                                icon: 'info',
                            });
                            $('#dgstudent').datagrid('reload');
                            
                        },
                        error: function() {
                            $.messager.alert({title: '提示',
                                msg: data.msg,
                                icon: 'info',
                            });
                            if (type == 1 || type == 2) {
                                $('#dgtruck').datagrid('reload');
                            } else {
                                $('#dgdriver').datagrid('reload');
                            }
                        }
                    });
                });
            });
            function calMinusImpurity(a, b) {
                //alert(a+' '+b);
                var grossWeight = $('#grossWeight').numberbox('getValue'); //onblue时间中获取的竟然是老值，a.target.value是新值               
                var tareWeight = $('#tareWeight').numberbox('getValue');
                var netWeight = $('#netWeight').numberbox('getValue');
                if (grossWeight != null && tareWeight != null && netWeight != null &&
                        grossWeight != '' && tareWeight != '' && netWeight != '') {
                    var v = grossWeight - tareWeight - netWeight;
                    $('#minusImpurity').numberbox('setValue', v);
                }
            }
            function anchorPic(value, row) {
                //var time = row.arrivalTime.subString(0,6);
                //var factoryCode=row.factoryCode;
                if (value != null && value != '') {
                    //return "<a target='view_window' href='../../photo/"+
                    //        time+"/"+factoryCode+"/'+value>"+"√"+"</a>";  //图片链接
                    return "<a target='view_window' href='../../photo/"
                            + value + "'>" + "√" + "</a>";  //图片链接
                } else {
                    return "<font color='red'>" + "×" + "</font>";
                }
            }
            function exportExcel() {
                var options = $('#dgstudent').datagrid('getPager').data("pagination").options;
                var pageNumber = options.pageNumber;
                var pageSize = options.pageSize;  //导出excel不需要分页

                var params = {searchKey: $('#fuzzyWord').textbox('getValue'),
                    factoryCode: $('#factoryCode').combobox('getValue'),
                    companyCode: $('#companyCode').combobox('getValue'),
                    dateFrom: $('#ddFrom').datebox('getValue'),
                    dateTo: $('#ddTo').datebox('getValue'),
                    page: pageNumber,
                    rows: pageSize};
                var urlStr = toUrlParamString(params);
                //alert(urlStr);
                var url = '../../servlet/util/ExcelFileCreate?' + urlStr;
                //open(encodeURI(url));
                open(url);
            }
            function exportPics() {
                var options = $('#dgstudent').datagrid('getPager').data("pagination").options;
                var pageNumber = options.pageNumber;
                var pageSize = options.pageSize;  //导出图片不需要分页
                var params = {searchKey: $('#fuzzyWord').textbox('getValue'),
                    factoryCode: $('#factoryCode').combobox('getValue'),
                    companyCode: $('#companyCode').combobox('getValue'),
                    dateFrom: $('#ddFrom').datebox('getValue'),
                    dateTo: $('#ddTo').datebox('getValue'),
                    page: pageNumber,
                    rows: pageSize};
                var urlStr = toUrlParamString(params);
                var url = "../../servlet/util/DownloadAll?" + urlStr;
                //open(encodeURI(url));
                open(url);
            }

            function toUrlParamString(param, key) {
                var paramStr = "";
                if (param instanceof String || param instanceof Number || param instanceof Boolean) {
                    paramStr += "&" + key + "=" + encodeURIComponent(param);
                } else {
                    $.each(param, function(i) {
                        var k = key == null ? i : key + (param instanceof Array ? "[" + i + "]" : "." + i);
                        paramStr += '&' + toUrlParamString(this, k);
                    });
                }
                return paramStr.substr(1);
            }
            ;

            function doSearch() {
                $('#dgstudent').datagrid('load', {
                    searchKey: $('#fuzzyWord').textbox('getValue'),
                    factoryCode: $('#factoryCode').combobox('getValue'),
                    companyCode: $('#companyCode').combobox('getValue'),
                    dateFrom: $('#ddFrom').datebox('getValue'),
                    dateTo: $('#ddTo').datebox('getValue'),
                });
            }
            function doSearchAll() {
                $('#fuzzyWord').textbox('clear');
                $('#factoryCode').combobox('clear');
                $('#companyCode').combobox('clear');
                $('#ddFrom').datebox('clear');
                $('#ddTo').datebox('clear');
                $('#dgstudent').datagrid('load', {
                    searchKey: $('#fuzzyWord').textbox('getValue'),
                    factoryCode: $('#factoryCode').combobox('getValue'),
                    companyCode: $('#companyCode').combobox('getValue'),
                    dateFrom: $('#ddFrom').datebox('getValue'),
                    dateTo: $('#ddTo').datebox('getValue'),
                });
            }
            var url;
            function newStudent() {
                var dd = $('#dgstudent').datagrid('getData')
                $('#dlg').dialog('open').dialog('setTitle', '创建');
                $('#fm').form('clear');
                //$('#studentno1').textbox('readonly',false);                
                url = '../../servlet/web/cargo/Save';
            }
            function editStudent() {
                var row = $('#dgstudent').datagrid('getSelected');
                if (row) {
                    $('#dlg').dialog('open').dialog('setTitle', '编辑');
                    $('#fm').form('clear');
                    $('#fm').form('load', row);
                    url = '../../servlet/web/cargo/Update';
                }
            }
            function saveStudent() {
                //alert(url);
                $('#fm').form('submit', {
                    url: url,
                    onSubmit: function() {
                        return $(this).form('validate');
                    },
                    success: function(result) {
                        var result = eval('(' + result + ')');
                        if (result.errorMsg) {
                            $.messager.show({
                                title: 'Error',
                                msg: result.errorMsg
                            });
                        } else {
                            $('#dlg').dialog('close');        // close the dialog
                            $('#dgstudent').datagrid('reload');    // reload the user data
                        }
                    }
                });
            }
            function destroyStudent() {
                var row = $('#dgstudent').datagrid('getSelected');
                if (row) {
                    $.messager.confirm('提示', '您确定要删除该记录吗?', function(r) {
                        if (r) {
                            $.messager.confirm('二次提示', "<font color='red'>您确定要删除该记录吗?</font>", function(r) {
                                if (r) {
                                     $.post('../../servlet/web/cargo/RemoveCargo',{ID: row.ID}, 
                                     function(result) {
                                        if (result.success) {
                                        $('#dgstudent').datagrid('reload');    // reload the user data
                                        } else {
                                            $.messager.show({// show error message
                                                title: 'Error',
                                                msg: result.errorMsg
                                        });
                                        }
                                    }, 'json');   
                                }
                            });
                        }
                    });
                }
            }
            function uploadPhoto( ) {
                var row;
                var msg;
                var photoid;
                var typeName;
                var row = $('#dgstudent').datagrid('getSelected');
                if (!row) {
                    $.messager.alert({title: '提示',
                        msg: '请选择一条记录操作',
                        icon: 'info',
                    });
                    return;
                }
                $('#fm-upload').form('clear');
                //$('#photoId').textbox('setValue',photoid);
                $('#fm-upload').form('load', row);
                $('#dlg-upload').dialog('open').dialog('setTitle', '上传照片');
            }
        </script>
    </head>
    <body class="easyui-layout">        
        <table id="dgstudent" title="业务数据维护" class="easyui-datagrid"
               url="../../servlet/web/cargo/Load"
               toolbar="#toolbar"
               rownumbers="true" fitColumns="true" singleSelect="true">
            <thead>
                <tr>                   
                    <th field="status" hidden="true">ID</th>
                    <th field="ID" hidden="true">ID</th>
                    <th field="recordNo" width="200">编号</th>
                    <th field="arrivalTime" width="100" hidden="true">时间</th>                    
                    <th field="factoryName" width="120">客户</th>
                    <th field="plateNo" width="80">车牌号</th>
                    <th field="productName" width="80">货物</th>
                    <th field="grossWeight" width="80">毛重(吨)</th>
                    <th field="tareWeight" width="80">皮重(吨)</th>
                    <th field="minusImpurity" width="80">扣杂(吨)</th>                    
                    <th field="netWeight" width="80">净重(吨)</th>
                    <th field="frontPhoto" width="25" data-options="formatter:anchorPic">头</th>
                    <th field="sidePhoto" width="25" data-options="formatter:anchorPic">货</th>
                    <th field="licensePhoto" width="25" data-options="formatter:anchorPic">证</th>
                    <th field="paperPhoto" width="25" data-options="formatter:anchorPic">单</th>
                    <th field="companyName" width="100">供应商</th>
                    <th field="siterName" width="80">驻厂员</th>
                </tr>
            </thead>
        </table>
        <div id="toolbar">            
            <select class="easyui-combobox" name="companyCode" id="companyCode"                     
                    style="width:250px"
                    data-options="valueField:'code',textField:'name',prompt:'请选择供应商',
                    url:'../../servlet/web/mycompany/Load4Combo',
                    editable:false">
                <option>请选择供应商</option>
            </select>           
            <select class="easyui-combobox" name="factoryCode" id="factoryCode" 
                    validType="selectValueRequired['#factoryCode']"
                    style="width:250px"
                    data-options="valueField:'code',textField:'name',prompt:'请选择客户',
                    url:'../../servlet/web/factory/Load4Combo',
                    editable:true">
                <option>请选择客户</option>
            </select>           

            <input id="ddFrom" type="text" class="easyui-datebox"  data-options="prompt:'选择查询开始日期'">
            <input id="ddTo" type="text" class="easyui-datebox"  data-options="prompt:'选择查询结束日期'">
            <input id="fuzzyWord" class="easyui-textbox" style="margin-left:20px;line-height:26px;border:1px solid #ccc" data-options="prompt:'输入查询关键字'">
            <a href="#" class="easyui-linkbutton" iconCls="icon-search" plain="true" onclick="doSearch()">查询</a>
            <a href="#" class="easyui-linkbutton" iconCls="icon-all" plain="true" onclick="doSearchAll()">清空搜索条件</a>
            <a href="#" class="easyui-linkbutton" iconCls="icon-excel" plain="true" onclick="exportExcel()">导出</a>
            <a href="#" class="easyui-linkbutton" iconCls="icon-large-picture" plain="true" onclick="exportPics()">导出图片</a>
        </div>        
        <div id="dlg" class="easyui-dialog" style="width:500px;height:520px;padding:5px"
             closed="true" buttons="#dlg-buttons">
            <div class="ftitle">业务数据</div>
            <form id="fm" method="post"> 
                <div class="fitem" style="display:none" >
                    <label>ID:</label>
                    <input id="ID"  class="easyui-textbox" name="ID"  hidden="true" readonly="true" >
                </div>
                <div class="fitem">
                    <label>驻厂员:</label>
                    <select class="easyui-combobox" name="siterID" id="siterID" 
                            validType="selectValueRequired['#siterID']"
                            style="width:300px"
                            data-options="valueField:'ID',textField:'name',  
                            url:'../../servlet/web/siter/Load4Combo',
                            required:true,
                            prompt:'请选择驻厂员'">                        
                    </select>
                </div>
                <div class="fitem">
                    <label>进场时间:</label>
                    <input class="easyui-datetimebox"  id="arrivalTime" name="arrivalTime"
                           data-options="required:true,showSeconds:true,validType:'datetime'"  style="width:300px">
                </div>
                <div class="fitem">
                    <label>客户工厂:</label>  <!--下面的ID与页面工具栏的重复了，故改成factoryCode1-->
                    <select class="easyui-combobox" name="factoryCode" id="factoryCode1"   
                            validType="selectValueRequired['#factoryCode1']"
                            style="width:300px"
                            data-options="valueField:'code',textField:'name',                            
                            url:'../../servlet/web/factory/Load4Combo',
                            editable:true,
                            onSelect: function(rec){
                            $('#factoryTypeCode').textbox('setValue',rec.typeCode);
                            $('#productCode').combobox('clear');
                            var url = '../../servlet/web/product/LoadByFactory4Combo?factoryCode='+rec.code;
                            $('#productCode').combobox('reload', url);
                            },
                            required:true,prompt:'请选择客户'">        
                    </select>
                </div>
                <div class="fitem" style="display:none">
                    <label>客户类别:</label>
                    <input id="factoryTypeCode" name="factoryTypeCode" class="easyui-textbox"   required="true">
                </div>
                <div class="fitem">
                    <label>车牌号:</label>
                    <input id="plateNo" name="plateNo" class="easyui-textbox" style="width:300px"  required="true">
                </div>
                <div class="fitem">
                    <label>供应商:</label>  <!--下面的ID与页面工具栏的重复了，故改成companyCode1-->
                    <select class="easyui-combobox" name="companyCode" id="companyCode1" 
                            validType="selectValueRequired['#companyCode1']"
                            style="width:300px"
                            data-options="valueField:'code',textField:'name',                            
                            url:'../../servlet/web/mycompany/Load4Combo',
                            editable:true,
                            prompt:'请选择供应商'">                        
                    </select>
                </div>                
                <div class="fitem">
                    <label>货物:</label>
                    <select class="easyui-combobox" name="productCode" id="productCode" 
                            validType="selectValueRequired['#productCode']"
                            style="width:300px"
                            data-options="valueField:'code',textField:'name',                            
                            editable:true,
                            prompt:'请选择货物'"> 
                    </select>
                </div>
                <div class="fitem">
                    <label>毛重(吨):</label>
                    <input id="grossWeight" name="grossWeight" type="text" class="easyui-numberbox" style="width:300px" data-options="min:0,precision:4">                    
                </div>
                <div class="fitem">
                    <label>皮重(吨):</label>
                    <input id="tareWeight"   name="tareWeight" type="text" class="easyui-numberbox" style="width:300px" data-options="min:0,precision:4">                    
                </div>
                <div class="fitem">
                    <label>净重(吨):</label>
                    <input id="netWeight" name="netWeight" type="text" class="easyui-numberbox" style="width:300px" data-options="onChange:calMinusImpurity,min:0,precision:4">                    
                </div>
                <div class="fitem">
                    <label>扣杂(吨):</label>
                    <input id="minusImpurity" name="minusImpurity" type="text" class="easyui-numberbox" readOnly="true" style="width:300px" data-options="precision:4">                    
                </div>                
            </form>
        </div>
        <div id="dlg-buttons">
            <a href="#" class="easyui-linkbutton" iconCls="icon-ok" onclick="saveStudent()">保存</a>
            <a href="#" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#dlg').dialog('close')">取消</a>
        </div>   
        <!--上传图片-->
        <div id="dlg-upload" class="easyui-dialog" style="width:500px;height:280px;padding:10px 20px"
             closed="true" buttons="#dlg-buttons-upload">
            <form id="fm-upload" method="post" enctype="multipart/form-data">                
                <div style="margin-bottom:20px">
                    <input class="easyui-textbox" id="ID" name="ID" type="text"  data-options="hidden:true,readonly:true"/>
                    <input class="easyui-textbox" id="recordNo" name="recordNo" type="text"  data-options="hidden:true,readonly:true"/>
                    <div class="fitem">
                        <input class="easyui-filebox" name="file1" id="file1" 
                               data-options="prompt:'选择车头照片...',validType:'isImage',buttonText:'选择文件'" 
                               style="width:100%" required="true">
                    </div>
                    <div class="fitem">
                        <input class="easyui-filebox" name="file2" id="file2" 
                               data-options="prompt:'选择货物照片...',validType:'isImage',buttonText:'选择文件'" 
                               style="width:100%" required="true">
                    </div>
                    <div class="fitem">
                        <input class="easyui-filebox" name="file3" id="file3" 
                               data-options="prompt:'选择证件照片...',validType:'isImage',buttonText:'选择文件'" 
                               style="width:100%" required="true">
                    </div>
                    <div class="fitem">
                        <input class="easyui-filebox" name="file4" id="file4" 
                               data-options="prompt:'选择榜单照片...',validType:'isImage',buttonText:'选择文件'" 
                               style="width:100%" required="true">
                    </div>
                </div>                
            </form>
        </div>
        <div id="dlg-buttons-upload">
            <a href="#" id="upload-photo" class="easyui-linkbutton" iconCls="icon-ok" >上传</a>
            <a href="#" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#dlg-upload').dialog('close')">取消</a>
        </div>
        <style type="text/css">
            #fm,#fm-upload{
                margin:0;
                padding:10px 30px;
            }
            .ftitle{
                font-size:14px;
                font-weight:bold;
                padding:5px 0;
                margin-bottom:10px;
                border-bottom:1px solid #ccc;
            }
            .fitem{
                margin-bottom:5px;
            }
            .fitem label{
                display:inline-block;
                width:80px;
            }
            .fitem input{
                width:160px;
            }
        </style>
        <script type="text/javascript">
            $(function() {
                var pager = $('#dgstudent').datagrid().datagrid('getPager');	// get the pager of datagrid
                pager.pagination({
                    buttons: [{
                            iconCls: 'icon-add',
                            text: '增加',
                            handler: function() {
                                newStudent();
                            }
                        }, {
                            iconCls: 'icon-edit',
                            text: '修改',
                            handler: function() {
                                editStudent();
                            }
                        }, {
                            iconCls: 'icon-photo',
                            text: '编辑照片',
                            handler: function() {
                                uploadPhoto();
                            }
                        }, {
                            iconCls: 'icon-remove',
                            text: '删除',
                            handler: function() {
                                destroyStudent();
                            }
                        }]
                });
            })
        </script>
    </body>
</html>
